package org.addressBook.servlet;

import org.addressBook.service.TbUserService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

public class RegistServlet extends HttpServlet {

    private TbUserService us = new TbUserService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        // 获取从前端传递过来的用户名与密码
        String name = request.getParameter("name");
        String mail = request.getParameter("mail");
        String password = request.getParameter("password");
        System.out.println("password = " + password + "====>" + name);
        // 校验用户信息是否为空
        if ("".equals(name) || "".equals(password)){
            out.print("用户名或密码未填，请重新注册！");
            request.getRequestDispatcher("/register.jsp").include(request, response);
            return;
        }
        try {
            us.regist(name,mail,password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        out.print("注册成功，请登录账号");
        request.getRequestDispatcher("/index.jsp").include(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
}
